import React from 'react';
import { connect } from 'dva';
import { WingBlank, WhiteSpace, Flex, List, TextareaItem, Checkbox, Icon, Button, Toast } from 'antd-mobile';
import { createForm } from 'rc-form';
import MemberInfo from 'components/MemberInfo';

const { Item } = List;
const { CheckboxItem, AgreeItem } = Checkbox;

const NativeScaleProgramePage = ({ dispatch, location, form, recids, intids, ...rest }) => {
  const { getFieldProps, getFieldError, validateFields, getFieldValue } = form;
  const { state: { ProgrameRec, ProgrameInt } } = location;
  const exitHandler = () => {
    //Toast.info('点击后退退出', 1);
    window.location = 'http://goBack';
  };
  const submit = () => {
    validateFields((errors, value) => {
      if (errors) {
        const fieldNames = Object.keys(errors);
        if (fieldNames[0]) {
          return Toast.fail(getFieldError(fieldNames[0]));
        }
      }

      value.programeint = rest.programeint.join(';');
      value.programerec = rest.programerec.join(';');
      const programerec = recids.join(";");
      const programeint = intids.join(";");
      const comment = getFieldValue('comment');
      const { type, doctorid, patientid, patientvalue, Report1, Report2, Report3,ProgrameRec,ProgrameInt } = location.state;
      
      const payload = {
        type,
        doctorid,
        patientid,
        patientvalue,
        report1: Report1,
        report2: Report2,
        report3: Report3,
        programeint,
        programerec,
        comment,
      };
      dispatch({
        type: 'scale/submitScalePrograme',
        payload,
      });
    });
    Toast.success('保存绑定成功', 1);
    window.location = 'http://goBack';
};
  return (
    <div>
      <MemberInfo {...location.state.member} />
      <WhiteSpace />
      <WingBlank size="md">
        <List>
          <Item extra={null}>
            <Flex justify="center">选择方案</Flex>
          </Item>
          
          {ProgrameInt.map(item => (
            <AgreeItem
              key={item.Id}
              onChange={e =>
                dispatch({
                  type: 'scale/savePrograme',
                  payload: {
                    type: 'programeint',
                    value: item.Id,
                  },
                })}
            >
              <Flex align="center" justify="between">
                {item.Title}
                <Flex align="center">
                  <a href={item.Url} target="_blank">查看方案</a>
                  <Icon type="right" style={{ fill: '#ccc' }} />
                </Flex>
              </Flex>
            </AgreeItem>
          ))}
          {ProgrameRec.map(item => (
            <AgreeItem
              key={item.Id}
              onChange={e =>
                dispatch({
                  type: 'scale/savePrograme',
                  payload: {
                    type: 'programerec',
                    value: item.Id,
                  },
                })}
            >
              <Flex align="center" justify="between">
                {item.Title}
                <Flex align="center">
                  <a href={item.Url} target="_blank">查看方案</a>
                  <Icon type="right" style={{ fill: '#ccc' }} />
                </Flex>
              </Flex>
            </AgreeItem>
          ))}
          <Item extra={null}>
            备注：
            <WhiteSpace />
            <TextareaItem
              {...getFieldProps('comment', {
                initialValue: '',
              })}
              rows={5}
              style={{ border: '1px solid #ddd' }}
            />
          </Item>
          <WhiteSpace size="xl" />
          <WingBlank>
            <Flex>
              <Flex.Item>
                <Button onClick={exitHandler}>退出</Button>
              </Flex.Item>
              <Flex.Item>
                <Button type="primary" onClick={submit}>保存</Button>
              </Flex.Item>
            </Flex>
          </WingBlank>
          <WhiteSpace size="xl" />
        </List>
      </WingBlank>
    </div>
  );
};

export default connect(({ scale }) => ({ ...scale }))(createForm()(NativeScaleProgramePage));
